$(function(){
	//modelStart();
	changeDevicetype();
	dictionaryStart("productionfactory","118001");
	//deviceclassesStart();
	unitStart();
	//xlStart();
	$("#linecode").bind('change',function(){
		gtStart();
	});
	$("#towercode").bind('change',function(){
		fallow();
	});
	$("#devicetype").bind('change',function(){
		changeDevicetype(false);
	});
	
	var requiredColumn = [
		"devicename"
		/*,"mainlinecode"
		,"localcity"
		,"operationunit"
		,"maintenanceteam"
		,"voltagelevel"
		,"commissioningdate"
		,"isgenerationdimension"
		,"isruralpowernetwork"
		,"equipmentstate"
		,"operationstate"
		,"regionalcharacteristics"
		,"importantdegree"
		,"equipmentowner"
		,"count"
		,"assetsnature"
		,"assetnumber"*/
	];
	addrequired(requiredColumn);
});

var deviceclassesStart=function(){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : '200001'
		},
		type : 'POST',
		success:function(list){
			if(null!=list && list.length>0){
				$("#deviceclasses").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				$('#deviceclasses').append(option);
			}
		}
	});
};
var unitStart=function(){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : '118002'
		},
		type : 'POST',
		success:function(list){
			if(null!=list && list.length>0){
				$("#unit").empty().show();
				var option="";
				var bl=false;
				for(var i=0;i<list.length;i++){
					if(unit && list[i].dictionaryValue==unit){
						bl=true;
					}
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				if(!bl && unit){
					option += '<option value="'+unit+'">'+unit+'</option>';
				}
				$('#unit').append(option);
				$('#unit').val(unit);
			}
		}
	});
};

var xlStart=function(){
	$.ajax({
		url : contextPath+'user/zyxl/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			devicetype : byqtype,
			transformercode : transformercode
		},
		async : false,
		success : function(data){
			if(data!=null&&data.length>0){
				var option="";
				$("#linecode").empty().show();
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#linecode").append(option);
			}
			setTimeout(function(){
				gtStart();
			},200);
		}
	});
}

var gtStart=function(){
	var linecode=$("#linecode").val();
	$.ajax({
		url : contextPath+'user/zygt/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			deviceid : linecode
		},
		async : false,
		success : function(data){
			if(data!=null&&data.length>0){
				$("#towercode").empty().show();
				var option="";
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#towercode").append(option);
			}
		}
	});
	/*$.ajax({
		url : contextPath+'user/zygt/load',
		type : 'post',
		data : {
			deviceId : linecode
		},
		success :function(data){
			if(data!=null){
				$("#voltagelevel").val(data.voltagelevel);
			}
		}
	});*/
}

function needadd(title,typeId,selectedId){
	var shield = document.createElement("DIV");
	shield.id = "shield";
	shield.style.position = "absolute";
	shield.style.left = "0px";
	shield.style.top = "0px";
	shield.style.width = "100%";
	shield.style.height = document.body.scrollHeight+"px";
	shield.style.background = "#333";
	shield.style.textAlign = "center";
	shield.style.zIndex = "10000";
	shield.style.filter = "alpha(opacity=30)";
	shield.style.MozOpacit=0.3;
	shield.style.opacity=0.3;
	var alertFram = document.createElement("DIV");
	alertFram.id="alertFram";
	alertFram.style.position = "absolute";
	alertFram.style.left = "55%";
	alertFram.style.top = "40%";
	alertFram.style.marginLeft = "-225px";
	alertFram.style.marginTop = "-75px";
	alertFram.style.width = "380px";
	alertFram.style.height = "200px";
	alertFram.style.background = "rgb(204, 204, 204)";
	alertFram.style.textAlign = "center";
	alertFram.style.lineHeight = "150px";
	alertFram.style.zIndex = "10001";
	strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
	strHtml += " <li style=\"background:#3dbfa7;text-align:left;padding-left:12px;font-size:16px;height:35px;line-height:35px;border:1px solid #F9CADE;\">[添加"+title+"]</li>\n";
	strHtml += " <li style=\"background:#fff;text-align:center;font-size:14px;height:150px;line-height:150px;border-left:1px solid #F9CADE;border-right:1px solid #3dbfa7;\"><span>名称：</span><input class=\"text\" type=\"text\" id=\"addcontent\"/></li>\n";
	strHtml += " <li style=\"background:#3dbfa7;text-align:center;font-weight:bold;height:35px;line-height:35px; border:1px solid #3dbfa7;\"><input type=\"button\" value=\"确 定\" onclick=\"doOk("+typeId+")\"  style=\"width:80px; height:24px; background:url(../images/add_facility.jpg) repeat-x 0 0; border:none; border-radius:5px;\" />";
	strHtml += "<input type=\"button\" value=\"取消\" onclick=\"doCancel()\"  style=\"width:80px; height:24px; margin-left:10px; background:url(../images/add_facility.jpg) repeat-x 0 0; border:none; border-radius:5px;\" /></li>\n";
	strHtml += "</ul>\n";
	alertFram.innerHTML = strHtml;
	document.body.appendChild(alertFram);
	document.body.appendChild(shield);
	var c = 0;
	this.doAlpha = function(){
		if (c++ > 20){clearInterval(ad);return 0;}
		shield.style.filter = "alpha(opacity="+c+")";
	}
	var ad = setInterval("doAlpha()",5);
	this.doOk = function(typeId){
		var dictionaryValue=$("#addcontent").val();
		if(dictionaryValue!=""&&dictionaryValue.length<12){
			$.ajax({
				url : contextPath+'user/dictionary/add',
				data : {
					dictionaryValue : dictionaryValue,
					typeId : typeId
				},
				type : 'post',
				success : function(data){
					alert(data.msgDesc);
					if(typeId==13001){
						modelStart();
					}else if(typeId==13002){
						productStart();
					}else if(typeId==118002){
						unitStart();
					}else{
						if(selectedId){
							changeSelect(selectedId,typeId);
						}
					}
				}
			});
		}
		alertFram.style.display = "none";
		shield.style.display = "none";
	};
	this.doCancel=function(){
		alertFram.style.display = "none";
		shield.style.display = "none";
	};
	alertFram.focus();
	document.body.onselectstart = function(){return false;};
}

function fallow(){
	var gtId=$('#towercode').val();
	deviceid=gtId;
	$.ajax({
		url : contextPath+'user/zygt/load',
		data : {
			deviceid : gtId
		},
		type : 'post',
		contentType: "application/x-www-form-urlencoded; charset=utf-8", 
		success : function(data){
			if(data!=null){
				$('#localcity').val(data.localcity);
				$('#operationunit').val(data.operationunit);
				$('#maintenanceteam').val(data.maintenanceteam);
			}
		}
	});
}

function add(){
	if(!validateForm()){
		return;
	}
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyzsfssb/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyzsfssblist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	if(!validateForm()){
		return;
	}
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyzsfssb/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyzsfssblist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}

function gettotalcapacity(){
	var singlecapacity=$('#singlecapacity').val();
	if(singlecapacity==null||singlecapacity==""){
		alert("请输入单只容量");
		return;
	}
	var thenumber=$("#thenumber").val();
	$("#groupcapacity").val(thenumber*parseFloat(singlecapacity));
	
}
function needadd1(title,selectID,obj){
	dictionaryadd(title,$(obj).attr("ref"),obj);
}
function deviceclassesSelect(data,bl){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : data
		},
		type : 'POST',
		success:function(list){
			if(null!=list && list.length>0){
				$("#deviceclasses").empty().show();
				var option="";
				var bl=false;
				for(var i=0;i<list.length;i++){
					if(deviceclasses && list[i].dictionaryValue==deviceclasses){
						bl=true;
					}
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				if(!bl && deviceclasses){
					option += '<option value="'+deviceclasses+'">'+deviceclasses+'</option>';
				}
				$('#deviceclasses').append(option);
				if(bl && deviceclasses)
				$('#deviceclasses').val(deviceclasses);
			}
		}
	});
}
function modelSelect(data){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : data
		},
		type : 'POST',
		success:function(list){
			if(null!=list && list.length>0){
				$("#model").empty().show();
				var option="";
				var bl=false;
				for(var i=0;i<list.length;i++){
					if(model && list[i].dictionaryValue==model){
						bl=true;
					}
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				if(!bl && model){
					option += '<option value="'+model+'">'+model+'</option>';
				}
				$('#model').append(option);
				$('#model').val(model);
			}
		}
	});
}
function changeDevicetype(bl){
	if(bl && devicetype){
		$('#devicetype').val(devicetype);
	}
	var _index = $("#devicetype option:selected")[0].index;
	if(_index < 10){
		_index = "0"+_index;
	}
	deviceclassesSelect(""+2+_index+"001",bl);
	//changeSelect("deviceclasses",""+2+_index+"001");
	$("#deviceclassesID").attr("ref",""+2+_index+"001");
	modelSelect(""+2+_index+"002");
	//changeSelect("model",""+2+_index+"002");
	$("#modelID").attr("ref",""+2+_index+"002");
}
function validateForm(){
	if($("#devicename").val()==""){
		alert("设备名称为空");
		return false;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return false;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return false;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return false;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return false;
	}
	if($("input[name='equipmentowner']").val()==""){
		alert("设备主人为空");
		return false;
	}
	if($("input[name='assetnumber']").val()==""){
		alert("资产编号为空");
		return false;
	}
	if(!isInt($("input[name='count']").val())){
		alert("数量只能为整数");
		return false;
	}*/
	return true;
}